package com.maezia.tdr.common;

import com.maezia.tdr.common.redis.BaseRedisRepository;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.concurrent.TimeUnit;

@Component
public class LockProvider extends BaseRedisRepository<String> {

    private static String LOCK_KEY = "LOCK" + CommonConstant.COLON;

    public LockProvider() {
        super(String.class);
    }


    public boolean lock(String key, String value, Long time, TimeUnit unit) {
        String redisKey = LOCK_KEY + key;
        return stringRedisValueOps.setIfAbsent(redisKey, value, time, unit);
    }

    public boolean unLock(String key){
        Boolean unLock = stringRedisTemplate.delete(LOCK_KEY + key);
        return ObjectUtils.isEmpty(unLock) ? false : unLock;
    }
}
